package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.Brand;
import com.kt.uokos.yimo.example.BrandExample;
import com.kt.uokos.yimo.mapper.BrandMapper;
import com.kt.uokos.yimo.service.BrandService;
import com.kt.uokos.yimo.util.RandomUtils;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandMapper brandMapper;

    @Override
    public List<Brand> querySelective(Integer id, String name, Integer page, Integer size, String sort, String order) {

        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            example.setOrderByClause(sort+" "+order);

        }
        PageHelper.startPage(page,size);
        return brandMapper.selectByExample(example);
    }

    @Override
    public List<Brand> queryList(Integer offset, Integer limit, String sort, String order) {
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(offset,limit);
        return brandMapper.selectByExample(example);
    }

    @Override
    public void create(Brand brand) {
        brand.setAddTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        brandMapper.insertSelective(brand);
    }

    @Override
    public Brand findById(Integer Id) {
        return brandMapper.selectByPrimaryKey(Id);
    }

    @Override
    public void deleteById(Integer id) {
         brandMapper.AdminDeleteByPrimaryKey(id);
    }

    @Override
    public int updateById(Brand brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return brandMapper.updateByPrimaryKeySelective(brand);
    }


}
